package com.zy.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import com.zy.entity.User;
import com.zy.service.UserService;
import com.zy.vo.DataView;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    /**
     * 验证码的逻辑
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/login/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //1. 创建一个验证码对象   HuTool定义图形验证码的长、宽、验证码位数、干扰线的条数
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 10);
        //2.将创建的验证码放入到session当中，并与前台session获取的验证码作比对
        session.setAttribute("code",captcha.getCode());
        //3.输出验证码
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);
        //4.关闭输出流
        outputStream.close();
    }

    /**
     * 具体的登录逻辑
     */
    @RequestMapping("/login/login")
    @ResponseBody
    public DataView login(String username, String password, String code, HttpSession session){
        DataView dataView = new DataView();
        //1.首先判断验证码对不对
        String sessionCode = (String) session.getAttribute("code");
        if (code != null  && sessionCode.equals(code)){
            //2.session普通登录逻辑
//            User user = userService.login(username,password);
            //shiro登录
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            subject.login(token);
            User user = (User) subject.getPrincipal();
            //3.判断
            if (user != null) {
                dataView.setCode(200);
                dataView.setMsg("登录成功！");
                //4. 放入session
//                session.setAttribute("user",user);
                session.setAttribute("username",username);
                //登录头像
//                session.setAttribute("img",user.getImg());
                return dataView;
            }else {
                dataView.setCode(100);
                dataView.setMsg("用户名或者密码错误，登录失败！");
            }
        }
        dataView.setCode(100);
        dataView.setMsg("验证码错误！");
        return dataView;
    }

    //登出方法
    @RequestMapping("/login/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }







}
